﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace SPRepository
{
    public class SPGenericListRepository<TT, TD> : SPGenericRepository<TT, TD>
        where TT : IContentType<TD>, new()
        where TD : class, IContentTypeDto
    {
        public SPGenericListRepository(SPList list)
            : base(list)
        {
        }

        public virtual void Create(TD dto)
        {
            if (List.BaseTemplate == SPListTemplateType.DocumentLibrary)
            {
                throw new InvalidOperationException("This method cannot be used on Document Libraries, please use the correct repository type");
            }

            if (dto == null)
            {
                return;
            }

            var contentTypeId = new TT().GetContentTypeIdDefinition();
            var item = List.Items.Add();
            item["ContentTypeId"] = contentTypeId;
            item.SystemUpdate();
            Update(item, dto);
        }
    }
}
